package cn.zhb.backend;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.TimeZone;

@SpringBootApplication
@Slf4j
public class BackendApplication {

	public static void main(String[] args) throws UnknownHostException {
		ConfigurableApplicationContext application = SpringApplication.run(BackendApplication.class, args);
		Environment env = application.getEnvironment();

		// 获取本机IP和主机名
		InetAddress localHost = InetAddress.getLocalHost();
		String ip = localHost.getHostAddress();
		String hostname = localHost.getHostName();  // 新增：获取主机名

		String active = env.getProperty("spring.profiles.active");
		String port = env.getProperty("server.port");
		port = port == null ? "8080" : port;
		String path = env.getProperty("server.servlet.context-path");
		path = path == null ? "" : path;

		log.info("\n----------------------------------------------------------\n\t" +
				"sampleManagementSystem is running! \n\t" +
				"系统运行环境 : \t" + active + "\n\t" +
				"主机名 : \t" + hostname + "\n\t" +  // 新增：主机名信息
				"本地访问地址 : \thttp://localhost:" + port + path + "/\n\t" +
				"局域网访问地址 : \thttp://" + hostname + ":" + port + path + "/\n\t" +  // 推荐用主机名替代IP
				"外部访问地址 : \thttp://" + ip + ":" + port + path + "/\n\t" +
				"----------------------------------------------------------");
	}


	@PostConstruct
	void started() {
		//       TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
		//      TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
	}
}
